package com.intel.two.controller.information;


import com.intel.two.common.LayuiPageVo;
import com.intel.two.common.R;
import com.intel.two.pojo.information.News;
import com.intel.two.service.information.NewsService;
import com.intel.two.utils.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/news")
public class NewsController {

    @Autowired
    private NewsService newsService;

    /*查全部+分页*/
    @RequestMapping("/NewsList")
    public LayuiPageVo<News> lostPageData(@RequestParam Map<String, Object> params) {
        System.out.println(params);
        return newsService.newsPageData(params);
    }

    /*增加*/
    @PostMapping("/addNews")
    public R addNews(News news) {
        System.out.println(news);
        int result = newsService.addNews(news);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }
    @PostMapping("/uploadPic")
    public R uploadPic(MultipartFile file) throws Exception {
        String originalFilename = file.getOriginalFilename();
        String fileUrl = OssUtil.updateLoad(file.getInputStream(), originalFilename);
        return R.ok().data("url",fileUrl);
    }



    @GetMapping("/deleteNews")
    public R deleteNews(Integer news_id) {
        int result = newsService.deleteNews(news_id);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @GetMapping("/deleteNewsAll")
    public R deleteNewsAll(@RequestParam Map<String, Object> map) {
        int result = newsService.deleteNewsAll(map);
        if (result > 0) {
            return R.ok();
        } else {
            return R.error();
        }
    }
    @PostMapping("updateNews")
    public R updateNews(News news) {
        System.out.println(news);
        int result = newsService.updateNews(news);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}
